dojo.provide("uws.drawing.ConnectionManager");

dojo.declare('uws.drawing.ConnectionManager',null,{
	/*
		Adds, edits, gets, and deletes connection from the data store.
	*/
	idcounter:null,
	components:null,
	
	constructor:function(start){
		this.idcounter=start;
		this.components = [];
	},
	newItem:function(umltype,comp1_id, comp1_dir, comp2_id, comp2_dir,  stereotype, custom, diagram){
		dataStore.newItem(
			{
				id: 'connection_'+(this.idcounter),
				name: 'connection_'+(this.idcounter), 
				uwstype: "connection", 
				umltype: umltype, 
				ref1_id: comp1_id,
				ref1_dir: comp1_dir,
				ref2_id: comp2_id,
				ref2_dir: comp2_dir,
				cardinality1: 1,
				cardinality2: 1,
				stereotype: stereotype,
				description: '',
				custom: custom
			},
			{
				parent: diagram,
				attribute: "connections"
			}
		);
		
		dataStore.setValue(root,"conn_counter",this.idcounter+1);
		return 'connection_'+(this.idcounter++);
	},
	addItem:function(id,component){
		this.components[id] = component;
	},
	getById:function(id){
		return this.components[id];
	},
	removeItem:function(id){
		if(this.components[id]==null) return;
		console.debug("Remove: "+id);
		this.components[id].destroy();
		delete(this.components[id]);
	},
	destroyItem:function(id){
		this.removeItem(id);
		console.debug("Destroy: "+id);
		dataStore.fetchItemByIdentity({
			identity: id,
			onItem: function(item){
				if(!dataStore.deleteItem(item)){
					console.debug("ConnectionManager: Failed to delete.");
				}
			},
			onError:function(e){
				console.debug(e);
			}
		});
	},
	focus:function(connection){
		
	}
});